/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package interfaz;
import VO.*;
import control.controlInsertarDatos;
import interfaz.InterfazPrincipal;
import interfaz.ModeloTabla;
import java.util.ArrayList;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JOptionPane;

/**
 *
 * @author David
 */
public class FormularioDatos extends javax.swing.JPanel {

    private final int ENTRADA_EXITOSA = 1;
    private final int TEMPERATURA_INCORRECTA = -1;
    private final int PRESION_INCORRECTA = -2;
    private final int PLUVIOSIDAD_INCORRECTA = -3;
    /**
     * Creates new form FormularioDatos
     */
    private ModeloTabla modeloTabla;
    
    public FormularioDatos() {
        initComponents(); 
        Object filas [][]=new Object[Datos.getContenido().size()][4];
        int i=0,j=0;
        for (VariablesAmbientales vA: Datos.getContenido()){
            filas[j][0]=j+1;
            for(i=0; i < vA.getMedidas().size() ; i++) {
                filas[j][i+1]=vA.getMedidas().get(i).getValor();
            }
            j++; 
        }
        modeloTabla=new ModeloTabla(
            filas,
            new String [] {
                "#","Temperatura °K", "Presion Atmosferica atm", "Pluviosidad lts/dia"
            }
        );
        tableDatos.setModel(modeloTabla); 
        jScrollPane2.setViewportView(tableDatos);
        TableRowSorter<TableModel> ordenador = new TableRowSorter<TableModel>(tableDatos.getModel());
        tableDatos.setRowSorter(ordenador);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        labelTemperatura = new javax.swing.JLabel();
        textTemperatura = new javax.swing.JTextField();
        labelPresion = new javax.swing.JLabel();
        textPresion = new javax.swing.JTextField();
        labelPluviosidad = new javax.swing.JLabel();
        textPluviosidad = new javax.swing.JTextField();
        labelDatos = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableDatos = new javax.swing.JTable();
        buttonInsertar = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        labelTemperatura.setText("Temperatura ( °K )");

        labelPresion.setText("Presion Atmosferica (atm)");

        labelPluviosidad.setText("Pluviosidad (lts/diario)");

        labelDatos.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        labelDatos.setText("INTRODUZCA LOS DATOS RECOGIDOS");

        tableDatos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Temperatura °C", "Presion Atmosferica atm", "Pluviosidad lts/año"
            }
        ));
        tableDatos.setColumnSelectionAllowed(true);
        jScrollPane2.setViewportView(tableDatos);
        tableDatos.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        buttonInsertar.setText("Insertar");
        buttonInsertar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonInsertarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(labelDatos)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelTemperatura)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(labelPluviosidad)
                                .addGap(16, 16, 16)
                                .addComponent(textPluviosidad, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(labelPresion)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(textPresion, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 80, Short.MAX_VALUE)
                        .addComponent(buttonInsertar, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(28, 28, 28))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(labelDatos, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textTemperatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelPresion, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(textPresion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonInsertar, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelPluviosidad)
                    .addComponent(textPluviosidad, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(91, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void buttonInsertarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInsertarActionPerformed
        try {    
         
         Medida temperatura = FactoryMedidas.crearTemperatura();
         temperatura.setValor(Double.parseDouble(textTemperatura.getText().toString()));
         Medida presionAtmosferica = FactoryMedidas.crearPresion();
         presionAtmosferica.setValor(Double.parseDouble(textPresion.getText().toString()));
         Medida pluviosidad = FactoryMedidas.crearPluviosidad();
         pluviosidad.setValor(Double.parseDouble(textPluviosidad.getText().toString()));
         ArrayList<Medida> nuevasVariables = new ArrayList<Medida>();
         nuevasVariables.add(temperatura);
         nuevasVariables.add(presionAtmosferica);
         nuevasVariables.add(pluviosidad);
         VariablesAmbientales vA = new VariablesAmbientales();
         vA.setMedidas(nuevasVariables);
         controlInsertarDatos controlInsertar = new controlInsertarDatos();
         int resultadoInsertar = controlInsertar.validarEntrada(vA);
         if(resultadoInsertar == TEMPERATURA_INCORRECTA)
             JOptionPane.showMessageDialog(this,"Temperatura invalida (Recuerde que la unidad es °K no negativos)");
         else {
            if(resultadoInsertar == PRESION_INCORRECTA)
               JOptionPane.showMessageDialog(this, "La presion atmosferica no puede ser negativa");
            else { 
                if(resultadoInsertar == PLUVIOSIDAD_INCORRECTA)
                    JOptionPane.showMessageDialog(this, "La pluviosidad no puede ser negativa");
                else {
                       int tam = Datos.getContenido().size();
                       Object fila[] = {tam, temperatura.getValor(), presionAtmosferica.getValor(), pluviosidad.getValor()}; 
                       modeloTabla.addRow(fila);
                       tableDatos.setModel(modeloTabla);
                       jScrollPane2.setViewportView(tableDatos);
                }
            } 
         }
        }
        catch(Exception excepcion) {
         JOptionPane.showMessageDialog(this, "Entradas de datos invalida", "ERROR", JOptionPane.ERROR_MESSAGE);      
        }
        textTemperatura.setText("");
        textPresion.setText("");
        textPluviosidad.setText("");
        
   
    }//GEN-LAST:event_buttonInsertarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonInsertar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel labelDatos;
    private javax.swing.JLabel labelPluviosidad;
    private javax.swing.JLabel labelPresion;
    private javax.swing.JLabel labelTemperatura;
    private javax.swing.JTable tableDatos;
    private javax.swing.JTextField textPluviosidad;
    private javax.swing.JTextField textPresion;
    private javax.swing.JTextField textTemperatura;
    // End of variables declaration//GEN-END:variables
}
